package io.renren.modules.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.modules.shop.entity.*;
import io.renren.modules.shop.mapper.*;
import io.renren.modules.shop.service.GoodsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class GoodsOrderServiceImpl extends ServiceImpl<GoodsOrderMapper, GoodsOrder> implements GoodsOrderService {

    @Autowired
    private GoodsAndOrderMapper goodsAndOrderMapper;

    @Autowired
    private GoodsMsgMapper goodsMsgMapper;

    @Autowired
    private GoodsCouponMapper goodsCouponMapper;

    @Autowired
    private UserAndCouponMapper userAndCouponMapper;

    @Autowired
    private GoodsPicMapper goodsPicMapper;

    @Override
    public int updateBatch(List<GoodsOrder> list) {
        return baseMapper.updateBatch(list);
    }

    @Override
    public int batchInsert(List<GoodsOrder> list) {
        return baseMapper.batchInsert(list);
    }

    @Override
    public int updateBatchSelective(List<GoodsOrder> list) {
        return baseMapper.updateBatchSelective(list);
    }

    @Override
    public boolean buyCar(List<GoodsMsg> goodsMsgs, Integer coupon, String openid) {
        GoodsOrder goodsOrder = new GoodsOrder();
        BigDecimal orderAllPrice = new BigDecimal(0);
        BigDecimal orderEndPrice = new BigDecimal(0);
        BigDecimal bargainPrice = new BigDecimal(0);
        goodsOrder.setOrderStatus(1); // 设置为代付款状态
        goodsOrder.setOpenid(openid); // 设置角色的openId
        goodsOrder.setOrderEndPrice(orderEndPrice); // 设置最终价格
        goodsOrder.setOrderAllPrice(orderAllPrice); // 设置总价格
        goodsOrder.setOrderBargainPrice(bargainPrice); // 设置优惠价格
        baseMapper.insert(goodsOrder);
        // 循环里面的商品
        for (GoodsMsg goodsMsg : goodsMsgs) {
            GoodsAndOrder goodsAndOrder = new GoodsAndOrder();
            goodsAndOrder.setGoodsId(goodsMsg.getGoodsId());
            goodsAndOrder.setGoodsPrice(goodsMsg.getGoodsPrice());
            goodsAndOrder.setOrderId(goodsOrder.getOrderId());
            goodsAndOrder.setOpenid(openid);
            goodsAndOrder.setGoodsNum(goodsMsg.getNum());
            goodsAndOrder.setGoodsStatus(1);  // 设置为卖出状态
            goodsAndOrder.setGoodsAllPrice(BigDecimal.valueOf(goodsMsg.getNum()).multiply(goodsMsg.getGoodsPrice()));
            // 减去商品库存操作
            goodsMsg.setGoodsNum(goodsMsg.getGoodsNum() - goodsAndOrder.getGoodsNum());
            if (goodsMsg.getGoodsNum() == 0) {
                goodsMsg.setGoodsStatus(1);
            }
            // 更新库存
            goodsMsgMapper.updateById(goodsMsg);
            // 更新购物车/订单表
            goodsAndOrderMapper.insert(goodsAndOrder);

            // 查询上述总订单，用于修改数据
            GoodsOrder order = baseMapper.selectById(goodsOrder.getOrderId());
            // 修改订单总表的数据
            order.setOrderAllPrice(goodsAndOrder.getGoodsAllPrice().add(order.getOrderAllPrice()));
            // 更新订单总表
            baseMapper.updateById(order);
        }

        GoodsOrder updateOrder = baseMapper.selectById(goodsOrder.getOrderId());
        if (coupon != 0) {
            GoodsCoupon goodsCoupon = goodsCouponMapper.selectById(coupon);
            updateOrder.setOrderEndPrice(updateOrder.getOrderAllPrice().subtract(goodsCoupon.getCouponSize()));
            updateOrder.setOrderBargainPrice(goodsCoupon.getCouponSize());
            updateOrder.setCouponId(coupon);
            // 将使用过的优惠券和用户进行绑定，用于验证是否使用过次优惠券
            UserAndCoupon userAndCoupon = new UserAndCoupon();
            userAndCoupon.setOpenid(openid);
            userAndCoupon.setCouponId(coupon);
            // 保存信息
            userAndCouponMapper.insert(userAndCoupon);
        } else {
            updateOrder.setOrderEndPrice(updateOrder.getOrderAllPrice());
        }
        baseMapper.updateById(updateOrder);
        return true;
    }

    @Override
    public List<GoodsOrder> getList(String openId) {
        List<GoodsOrder> goodsOrders = this.baseMapper.selectList(new QueryWrapper<GoodsOrder>().lambda().eq(GoodsOrder::getOpenid,openId));
        for (GoodsOrder goodsOrder : goodsOrders) {
            List<GoodsAndOrder> goodsAndOrders = goodsAndOrderMapper.selectList(new QueryWrapper<GoodsAndOrder>().lambda().eq(GoodsAndOrder::getOrderId, goodsOrder.getOrderId()));
            List<Integer> collect = goodsAndOrders.stream().map(GoodsAndOrder::getGoodsId).collect(Collectors.toList());
            if (!collect.isEmpty()) {
                List<GoodsMsg> goodsMsgs = goodsMsgMapper.selectBatchIds(collect);
                for (GoodsMsg msg : goodsMsgs) {
                    GoodsPic goodsPic = new GoodsPic();
                    goodsPic.setGoodsId(msg.getGoodsId());
                    GoodsPic one = goodsPicMapper.selectOne(new QueryWrapper<GoodsPic>(goodsPic));
                    msg.setPicPosition(Arrays.asList(one.getPicPath().split(",")));
                }
                goodsOrder.setList(goodsMsgs);
            }
        }
        return goodsOrders;
    }
}



